<table>
    <tr>
        <td colspan="8">
            <?php echo $this->element('excel/title'); ?>
        </td>
        <td colspan="4">
            <table>
                <tr>
                    <td colspan="4" class="docNote">VMI REPORT: Customer Stock out<br />Group by Cost Center</td>
                </tr>
                <tr>
                    <td class="label">Report date</td>
                    <td><?php echo $report_date; ?></td>
                    <td class="label">Report by</td>
                    <td><?php echo $user; ?></td>
                </tr>
                <tr>
                    <td width="100px" class="label">Customer no.</td>
                    <td colspan="3"><?php echo $customer_no; ?></td>
                </tr>
                <tr>
                    <td class="label">Customer name</td>
                    <td colspan="3"><?php echo $customer_name; ?></td>
                </tr>
                <tr>
                    <td class="label">Site</td>
                    <td colspan="3">
                        <?php echo $site_no; ?>&nbsp;
                        <?php echo $site_name; ?>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
 <table>
            <?php if (!empty($cost_center_name)) : ?>
            <tr>
                <td width="120px"><label>Cost Center</label></td>
                <td><?php echo $cost_center_name; ?></td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($criteria['CustStockOutHead']['stock_out_date_start'])) : ?>
            <tr>
                <td width="120px"><label>Stock out date</label></td>
                <td>From <?php echo $criteria['CustStockOutHead']['stock_out_date_start']; ?> to <?php echo $criteria['CustStockOutHead']['stock_out_date_end']; ?></td>
            </tr>
            <?php endif; ?>
        </table>

<!--div class="header">
</div-->

<div class="main">
    <table id="dataTable" class="dataTable" cellspacing="0" cellpadding="0">
        <thead>
            <tr>
                <th class="no" rowspan="2">No.</th>
                <th rowspan="2">Cost Center</th>
                <th rowspan="2">Stock code</th>
                <th rowspan="2">Vendor Part no.</th>
                <?php if ($display_stock_out_no == true) { ?>
                    <th rowspan="2">Stock out no.</th>
                    <th rowspan="2">Stock out date</th>
                <?php } ?>
                <th rowspan="2">Vendor</th>
                <th rowspan="2">Product Description</th>
                <?php if ($display_stock_out_no == true) { ?>
                    <th rowspan="2">Remark</th>
                <?php } ?>
                <th rowspan="2" width="40px">Freq.</th>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <th rowspan="2" width="45px">Price</th>
                <?php endif; ?>
                <th colspan="3">Quantity</th>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <th rowspan="2" width="50px">Amount</th>
                <?php endif; ?>
            </tr>
            <tr>
                <th width="50px">Stock out</th>
                <th width="50px">Return</th>
                <th width="50px">Net</th>
            </tr>
        </thead>
        <tbody>
            <?php $i = 0;
            $r = 0;
            $total_freq = 0;
            $total_qty = 0;
            $total_net_qty = 0;
            $total_return = 0;
            $total_amount = 0;
            foreach($this->data as $i => $data): ?>
            <tr>
                <td class="center"><?php echo $i + 1; ?></td>
                <td class="bold"><?php echo $data['SiteCostCenter']['name']; ?></td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <?php if ($display_stock_out_no == true) { ?>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                <?php } ?>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td class="bold"><?php echo $data['SiteCostCenter']['frequency']; ?></td>
                        <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="bold">-</td>
                        <?php endif; ?>
                <td class="bold"><?php echo $data['SiteCostCenter']['stock_out_qty']; ?></td>
                <td class="bold"><?php echo $data['SiteCostCenter']['return_qty']; ?></td>
                <td class="bold"><?php echo $data['SiteCostCenter']['net_stock_out_qty']; ?></td>
                        <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="bold"><?php echo number_format($data['SiteCostCenter']['amount'], 2); ?></td>
                        <?php endif; ?>
                        <?php
                        $total_freq += $data['SiteCostCenter']['frequency'];
                        $total_qty += $data['SiteCostCenter']['stock_out_qty'];
                        $total_return += $data['SiteCostCenter']['return_qty'];
                        $total_net_qty += $data['SiteCostCenter']['net_stock_out_qty'];
                        $total_amount += $data['SiteCostCenter']['amount'];
                        ?>
            </tr>
            <?php foreach($data['CustProductSite'] as $cps) : ?>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>
                        <?php echo $cps['CustProductSite']['stock_code']; ?>
                </td>
                <td>
                        <?php echo $cps['CustProductSite']['dispatch_from'] == ITS_PREFIX? $cps['Product']['part_no']: $cps['Product']['supplier_part_no']; ?>
                </td>
                <?php if ($display_stock_out_no == true) { ?>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                <?php } ?>
                <td>
                        <?php echo $cps['CustStockOutHead'][0]['dispatch_from'] == ITS_PREFIX?
                        $this->requestAction('system_preferences/get/company_name'): $cps['Supplier']['name']; ?>
                </td>
                <td><?php echo $cps[0]['text']; ?></td>
                <?php if ($display_stock_out_no == true) { ?>
                    <td>&nbsp;</td>
                <?php } ?>
                <td class="text-right"><?php echo $cps[0]['frequency']; ?></td>
                    <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right"><?php echo number_format($cps[0]['price'], 2); ?></td>
                    <?php endif; ?>
                <td class="text-right"><?php echo $cps[0]['stock_out_qty']; ?></td>
                <td class="text-right"><?php echo $cps[0]['return_qty']; ?></td>
                <td class="text-right"><?php echo $cps[0]['net_stock_out_qty']; ?></td>
                    <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right"><?php echo number_format($cps[0]['price'] * $cps[0]['net_stock_out_qty'], 2); ?></td>
                    <?php endif; ?>
            </tr>
                <?php if ($display_stock_out_no == true) : ?>
                    <?php
                    // ตัวแปรสำหรับลำดับเลข stock out no
                    $k = 1;
                    foreach($cps['CustStockOutHead']  as $sto) :
                        ?>
            <tr>
                <td class="rowTd">&nbsp;</td>
                <td class="rowTd">&nbsp;</td>
                <td class="rowTd">&nbsp;</td>
                <td class="rowTd">&nbsp;</td>
                            <?php if ($display_stock_out_no == true) : ?>
                <td><span><?php echo $sto['stock_out_no']; ?></span></td>
                <td><?php echo $sto['stock_out_date']; ?></td>

                <td class="rowTd">&nbsp;</td>
                <td class="rowTd">&nbsp;</td>
                <td>&nbsp;<?php echo !empty($sto['remark'])? '-&nbsp;' . $sto['remark']: ''; ?></td>
                <td>&nbsp;</td>
                                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td>&nbsp;</td>
                                <?php endif; ?>
                <td class="text-right"><?php echo $sto['stock_out_qty']; ?></td>
                <td class="text-right"><?php echo $sto['return_qty']; ?></td>
                <td class="text-right"><?php echo $sto['net_stock_out_qty']; ?></td>
                                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right"><?php echo number_format($sto['net_stock_out_qty'] * $cps[0]['price'], 2); ?></td>
                                <?php endif; ?>
                            <?php endif; ?>
            </tr>
                        <?php ++$k;
                    endforeach; ?>
                <?php endif; ?>
            <?php endforeach;
            endforeach; ?>
            <tr class="trFooter">
                <?php $colspan = 6;
                if ($display_stock_out_no == true) {
                    $colspan = 9;
                } ?>
                <td colspan="<?php echo $colspan; ?>" class="text-right">Grand Total</td>
                <td class="text-right bold"><?php echo number_format($total_freq); ?></td>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right">-</td>
                <?php endif; ?>

                <td class="text-right bold"><?php echo number_format($total_qty); ?></td>
                <td class="text-right bold"><?php echo number_format($total_return); ?></td>
                <td class="text-right bold"><?php echo number_format($total_net_qty); ?></td>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right bold"><?php echo number_format($total_amount, 2); ?></td>
                <?php endif; ?>
            </tr>
        </tbody>
    </table>
</div>

<div class="footer"></div>

<div id="divLastFooter"></div>